#! /usr/bin/python

# Llibreries
import cgi, os, re, sys, string, time

model = {
"54" : 100,
"71" : 200,
"82" : 300,
"139" : 600 }

model_names = {
"54" : "Economic",
"71" : "Semi-Luxe",
"82" : "Luxe",
"139" : "Limusina"
}

motor = {
"Diesel" : 1.1,
"Gasolina" : 0.8 }

def print_error(reason):
    print "<TITLE>Hay que engrasarlo!!</TITLE>\n"
    print "<H1>Error: You did not provide a %s\n"%reason
    sys.exit()

# Programa principal
print "Content-type: text/html\n\n"
form = cgi.FieldStorage()

required_fields = ("model_vehicle", "sub_model_vehicle", "dies_lloguer", "num_vehicles", "descompte")

for k in required_fields:
    if not form.has_key(k):
        print_error(k)

model_vehicle = form.getvalue('model_vehicle', '')
sub_model_vehicle = form.getvalue('sub_model_vehicle', '')
dies_lloguer = form.getvalue('dies_lloguer', '')
num_vehicles = form.getvalue('num_vehicles', '')
descompte = form.getvalue('descompte', '')

print "<h1>Reserva Realitzada amb exit! </h1>"
print "<p>Model: " + model_names[model_vehicle] + "</p>"
print "<p>Motor: " + sub_model_vehicle + "</p>"
print "<p>Dies: " + dies_lloguer + "</p>"
print "<p>Quantitat: " + num_vehicles + "</p>"
print "<p>Descompte: " + descompte + "</p>"

aux = model[model_vehicle] * motor[sub_model_vehicle] * float(dies_lloguer) * float(num_vehicles) * (1 - float(descompte)/100)
data = open("transaccions.txt","a")
data.write(model_names[model_vehicle] + " " + sub_model_vehicle + " " + dies_lloguer + " " + num_vehicles + " " +descompte + " " + str(aux) +"\n")

print "<h2>Preu: "
print aux
print " Euros</h2>"

print "<a href=\"/main.html\">Tornar a la plana principal</a>"

